package com.spx.kernel;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor
{

	private static final String STATUS_SESSION_INFO_EXTENSIONPOINT_ID = "com.spx.kernel.ui.status";
	private static final String CLASS = "class";
	ApplicationActionBarAdvisor applicationActionBarAdvisor;

	public ApplicationActionBarAdvisor getApplicationActionBarAdvisor()
	{
		return applicationActionBarAdvisor;
	}

	public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer)
	{
		super(configurer);
		
	}

	public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer)
	{
		applicationActionBarAdvisor = new ApplicationActionBarAdvisor(configurer);

		
		return applicationActionBarAdvisor;
	}

	public void preWindowOpen()
	{
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(500, 400));
		configurer.setShowCoolBar(false);
		configurer.setShowStatusLine(true);
	}

	@Override
	public void postWindowCreate()
	{
		super.postWindowCreate();
		IStatusLineManager statusLine = getWindowConfigurer().getActionBarConfigurer().getStatusLineManager();
		getStatusSessionHolder().setStatusLine(statusLine);

	}

	public static   StatusSessionHolder getStatusSessionHolder()
	{
		final IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(STATUS_SESSION_INFO_EXTENSIONPOINT_ID);
		StatusSessionHolder result = null;
		final IExtension[] extensions = extensionPoint.getExtensions();
		for (final IExtension extension : extensions)
		{
			final IConfigurationElement[] configs = extension.getConfigurationElements();
			for (final IConfigurationElement config : configs)
			{
				try
				{
					final StatusSessionHolder sessionHolder = (StatusSessionHolder) config.createExecutableExtension(CLASS);
					if (sessionHolder != null)
					{
						result = sessionHolder;
					}

				} catch (final CoreException e)
				{
					e.printStackTrace();
				}
			}
		}

		return result;
	}

}
